#! /bin/sh
# intltoolize - Prepare a package to use intltool.
# @configure_input@
# Copyright (C) 1996-1999 Free Software Foundation, Inc.
# Copyright (C) 2001 Eazel, Inc.
#
# Originally based on libtoolize by Gordon Matzigkeit <gord@gnu.ai.mit.edu>, 1996
#
# Modified for xml-i18n-tools by Maciej Stachowiak <mjs@noisehavoc.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# As a special exception to the GNU General Public License, if you
# distribute this file as part of a program that contains a
# configuration script generated by Autoconf, you may include it under
# the same distribution terms that you use for the rest of that program.

# The name of this program.
progname=`echo "$0" | sed 's%^.*/%%'`

# Constants.
PROGRAM=intltoolize
PACKAGE=@PACKAGE@
VERSION=@VERSION@

# Directory names.
prefix=@prefix@

case "`uname`" in
MINGW32*)
  # Assume intltoolize is in $prefix/bin. Strip off the script name and the "bin"
  prefix=`dirname $0`
  prefix=`dirname $prefix`
  ;;
esac

datarootdir=@datarootdir@
datadir=@datadir@
pkgdatadir=@pkgdatadir@

aclocaldir=${datadir}/aclocal
intltool_m4="${aclocaldir}/intltool.m4"

dry_run=no
help="Try '$progname --help' for more information."
rm="rm -f"
rm_rec="rm -rf"
ln_s="ln -s"
cp="cp -f"
mkdir="mkdir"
mkinstalldirs="mkinstalldirs"

# Global variables.
automake=
copy=
force=
status=0

for arg
do
  case "$arg" in
  --help)
    cat <<EOF
Usage: $progname [OPTION]...

Prepare a package to use intltool.

    --automake        work silently, and assume that Automake is in use
-c, --copy            copy files rather than symlinking them
    --debug           enable verbose shell tracing
-n, --dry-run         print commands rather than running them
-f, --force           replace existing files
    --help            display this message and exit
    --version         print version information and exit

You must 'cd' to the top directory of your package before you run
'$progname'.
EOF
    exit 0
    ;;

  --version)
    echo "$PROGRAM (GNU $PACKAGE) $VERSION"
    exit 0
    ;;

  --automake)
    automake=yes
    ;;

  -c | --copy)
    ln_s=
    ;;

  --debug)
    echo "$progname: enabling shell trace mode"
    set -x
    ;;

  -n | --dry-run)
    if test "$dry_run" != yes; then
      dry_run=yes
      rm="echo $rm"
      rm_rec="echo $rm_rec"
      test -n "$ln_s" && ln_s="echo $ln_s"
      cp="echo $cp"
      mkdir="echo mkdir"
      mkinstalldirs="echo $mkinstalldirs"
    fi
    ;;

  -f | --force)
    force=yes
    ;;

  -*)
    echo "$progname: unrecognized option '$arg'" 1>&2
    echo "$help" 1>&2
    exit 1
    ;;

  *)
    echo "$progname: too many arguments" 1>&2
    echo "$help" 1>&2
    exit 1
    ;;
  esac
done

if test -f configure.ac; then
    configure="configure.ac"
else
    if test -f configure.in; then
	configure="configure.in"
    else
	echo "$progname: neither 'configure.ac' nor 'configure.in' exists" 1>&2
	echo "$help" 1>&2
	exit 1
    fi
fi


files='po/Makefile.in.in'

if test -z "$automake"; then
  if egrep '^(AC|IT)_PROG_INTLTOOL' $configure >/dev/null 2>&1; then :
  else
    echo "ERROR: 'IT_PROG_INTLTOOL' must appear in $configure for intltool to work."
    exit 1
  fi

  if grep 'generated automatically by aclocal' aclocal.m4 >/dev/null 2>&1; then
    updatemsg="update your 'aclocal.m4' by running aclocal"
  else
    updatemsg="add the contents of '$intltool_m4' to 'aclocal.m4'"
  fi

  if egrep '^AC_DEFUN\(\[IT_PROG_INTLTOOL\]' aclocal.m4 >/dev/null 2>&1; then
    # Check the version number on intltool.m4 and the one used in aclocal.m4.
    instserial=`grep '^# serial ' $intltool_m4 | grep 'IT_PROG_INTLTOOL' | sed -e 's/^# serial \([0-9][0-9]*\).*$/\1/; q'`

    if test -z "$instserial"; then
      echo "$progname: warning: no serial number on '$intltool_m4'" 1>&2
    else
      # If the local macro has no serial number, we assume it's ancient.
      localserial=`grep '^# serial ' aclocal.m4 | grep 'IT_PROG_INTLTOOL' | sed -e 's/^# serial \([0-9][0-9]*\).*$/\1/; q'`

      test -z "$localserial" && localserial=0

      if test "$localserial" -lt "$instserial"; then
	echo "You should $updatemsg."
      elif test "$localserial" -gt "$instserial"; then
	echo "$progname: '$intltool_m4' is serial $instserial, less than $localserial in 'aclocal.m4'" 1>&2
	if test -z "$force"; then
	  echo "Use '--force' to replace newer intltool files with this version." 1>&2
	  exit 1
	fi
	echo "To remain compatible, you should $updatemsg."
      fi
    fi
  else
    echo "You should $updatemsg."
  fi
fi


# Copy or symlink the Makefile.in.in
(
for file in $files; do
  if test -f "$file" && test -z "$force"; then
    test -z "$automake" && echo "$progname: '$file' exists: use '--force' to overwrite" 1>&2
    continue
  fi

  $rm $file
  if test -n "$ln_s" && $ln_s $pkgdatadir/`basename $file` $file; then :
  elif $cp $pkgdatadir/`basename $file` $file; then :
  else
    echo "$progname: cannot copy '$pkgdatadir/`basename $file`' to '$file'" 1>&2
    status=1
  fi
# Touch script names for backward compatibility
  for script in intltool-extract.in intltool-merge.in intltool-update.in; do
      # Always remove the script files now
      rm -f $script
      if egrep $script Makefile.am >/dev/null 2>&1; then
	  touch $script
      fi
  done
done

exit $status
# make sure this subshell exits with the exit value if it failed
) || exit $?


# If the AC_CONFIG_MACRO_DIR() macro is used, copy intltool.m4 from our
# prefix to that directory.  This makes sure that the M4 macro used
# matches the intltool scripts we install.
# If AC_CONFIG_MACRO_DIR is not used, things will behave as before (aclocal
# will be used to pull in the macro.
m4dir=`cat "$configure" | grep '^AC_CONFIG_MACRO_DIR' | sed -n -e 's/AC_CONFIG_MACRO_DIR(\([^()]*\))/\1/p' | sed -e 's/^\[\(.*\)\]$/\1/' | sed -e 1q`
if test -n "$m4dir"; then
  rm -f $m4dir/intltool.m4
  if test -n "$ln_s" && $ln_s $intltool_m4 $m4dir/intltool.m4; then :
  elif $cp $intltool_m4 $m4dir/intltool.m4; then :
  else
    echo "$progname: cannot copy '$intltool_m4' to '$m4dir/intltool.m4'" 1>&2
    exit 1
  fi
fi


# FIXME: This probably does not work w/ builddir != srcdir because it
# gets at source files relative to the current directory.

grep INTLTOOL_MAKEFILE po/Makefile.in.in >/dev/null || {
  echo "$progname: 'po/Makefile.in.in' is out of date: use '--force' to overwrite"
  exit 1
}

exit $status

# Local Variables:
# mode:shell-script
# sh-indentation:2
# End:
